#include "mainwindow.h"

#include "core/sourcelist.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    playlist = new Playlist();

    initActions();
    initGUI();
    createConnections();

    Track t("E:\\06-Cape Fear.mp3");
    SourceList::getInstance()->addItem(new PlaylistItem(1,t));

}

MainWindow::~MainWindow()
{
}

void MainWindow::initGUI()
{
    initMenu();
    initToolbar();
    volSlider = new QSlider(Qt::Horizontal);
    QVBoxLayout *mainLayout = new QVBoxLayout();
    splitter = new QSplitter();

    QHBoxLayout *toolbarLayout = new QHBoxLayout();
    toolbarLayout->addWidget(toolbar);
    toolbarLayout->addSpacing(0);
    toolbarLayout->addWidget(volSlider);
    mainLayout->addLayout(toolbarLayout);
    //inittabPanel

    //initPlaylistWidget
    playlistScrollArea = new QScrollArea(this);
    playlistTab = new PlaylistTab();
    playlistScrollArea->setWidget(playlistWidget);
    playlistScrollArea->setWidgetResizable(true);
    splitter->addWidget(playlistScrollArea);
    //initslider
    //initlcdscreen
    //connect via layouts
    mainLayout->addWidget(splitter);
    QWidget *widget = new QWidget();
    widget->setLayout(mainLayout);
    setCentralWidget(widget);
}

void MainWindow::initMenu()
{
    QMenu *menu = new QMenu(tr("&File"), this);
    menu->addAction(actionLoadFiles);
    menu->addAction(actionLoadFolder);
    menu->addAction(actionQuit);
    menuBar()->addMenu(menu);

    menu = new QMenu(tr("&Playback"), this);
    menu->addAction(actionPreviousTrack);
    menu->addAction(actionPlay);
    menu->addAction(actionPause);
    menu->addAction(actionStop);
    menu->addAction(actionNextTrack);
    menu->addSeparator();

    QMenu *submenu=new QMenu("Repeat");
    submenu->addAction(actionNone);
    submenu->addAction(actionSingle);
    submenu->addAction(actionAlbum);
    submenu->addAction(actionAll);
    menu->addMenu(submenu);

    menu->addAction(actionShuffle);
    menu->addSeparator();
    menu->addAction(actionMute);
    menu->addAction(actionVolumeUp);
    menu->addAction(actionVolumeDown);
    menuBar()->addMenu(menu);

    menu = new QMenu(tr("&Playlist"), this);
    menu->addAction(actionOpenPlaylist);
    menu->addAction(actionSavePlaylist);
    menu->addAction(actionSelectAll);
    menu->addAction(actionClearAll);
    menuBar()->addMenu(menu);
}

void MainWindow::initToolbar()
{
    toolbar = new QToolBar(this);
    toolbar->addAction(actionPreviousTrack);
    toolbar->addAction(actionPlay);
    toolbar->addAction(actionPause);
    toolbar->addAction(actionStop);
    toolbar->addAction(actionNextTrack);
    toolbar->addSeparator();
    toolbar->addAction(actionRepeat);
    toolbar->addAction(actionShuffle);
}

void MainWindow::setCompactMode(bool)
{
    //
}

void MainWindow::initActions()
{
    //File menu
    actionLoadFiles = new QAction(QIcon("images/openfilebb.ico"),tr("&Load Files..."), this);
    actionLoadFiles->setStatusTip(tr("Open File"));
    actionLoadFolder = new QAction(tr("&Load Directory"), this);
    actionLoadFolder->setStatusTip(tr("Load Directory"));
    actionQuit = new QAction(tr("&Quit"), this);
    actionQuit->setStatusTip(tr("Quit"));
    //playback menu
    actionPreviousTrack = new QAction(tr("&Previous track"), this);
    actionPreviousTrack->setStatusTip(tr("Previous track"));
    actionPlay = new QAction(tr("&Play"), this);
    actionPlay->setStatusTip(tr("Play"));
    actionStop = new QAction(tr("&Stop"), this);
    actionStop->setStatusTip(tr("Stop"));
    actionNextTrack = new QAction(tr("&Next track"), this);
    actionNextTrack->setStatusTip(tr("Next track"));
    actionPause = new QAction(tr("&Pause"), this);
    actionPause->setStatusTip(tr("Pause"));
    actionShuffle = new QAction(tr("&Shuffle"), this);
    actionShuffle->setStatusTip(tr("Shuffle"));
    actionNone = new QAction(tr("&None"), this);
    actionNone->setStatusTip(tr("None"));
    actionSingle = new QAction(tr("&Single"), this);
    actionSingle->setStatusTip(tr("Single"));
    actionAlbum = new QAction(tr("&Album"), this);
    actionAlbum->setStatusTip(tr("Album"));
    actionAll = new QAction(tr("&All"), this);
    actionAll->setStatusTip(tr("All"));
    actionMute = new QAction(tr("&Mute"), this);
    actionMute->setStatusTip(tr("Mute"));
    actionVolumeUp = new QAction(tr("&Volume up"), this);
    actionVolumeUp->setStatusTip(tr("Play"));
    actionVolumeDown = new QAction(tr("&Volume down"), this);
    actionVolumeDown->setStatusTip(tr("Play"));
    //playlist menu
    actionOpenPlaylist = new QAction(tr("&Open playlist"), this);
    actionOpenPlaylist->setStatusTip(tr("Open playlist"));
    actionSavePlaylist = new QAction(tr("&Save playlist"), this);
    actionSavePlaylist->setStatusTip(tr("Save playlist"));
    actionSelectAll = new QAction(tr("&Select all"), this);
    actionSelectAll->setStatusTip(tr("Select all"));
    actionClearAll = new QAction(tr("&Clear all"), this);
    actionClearAll->setStatusTip(tr("Clear all"));
    //Buttons actions
    actionRepeat = new QAction(tr("&Repeat"), this);
    actionRepeat->setStatusTip(tr("Repeat"));
}

void MainWindow::createConnections()
{
    connect(actionPlay,SIGNAL(triggered()),playlist,SLOT(launchItem()));
}
